home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Cache / DB.php < prev    next >
PHP Script  |  2004-03-24  |  11KB  |  426 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: Cache                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de>               |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: DB.php,v 1.7 2003/01/04 11:54:45 mj Exp $
  19.  
  20. require_once 'Cache.php';
  21. require_once 'DB.php';
  22.  
  23. /**
  24. * Cache_DB
  25. *
  26. * @author       Sebastian Bergmann <sb@sebastian-bergmann.de>
  27. * @module       Cache_DB
  28. * @modulegroup  Cache_DB
  29. * @package      Cache
  30. * @version      $Revision: 1.7 $
  31. * @access       public
  32. */
  33. class Cache_DB extends Cache {
  34.     /**
  35.     * PEAR DB Object
  36.     *
  37.     * @var  object
  38.     */
  39.     var $db;
  40.  
  41.     /**
  42.     * Lifetime of a cached result set (in seconds)
  43.     *
  44.     * @var  integer
  45.     */
  46.     var $expires = 3600;
  47.  
  48.     /**
  49.     * PEAR DB DSN
  50.     *
  51.     * @var  string
  52.     */
  53.     var $dsn = '';
  54.  
  55.     /**
  56.     * PEAR DB Options
  57.     *
  58.     * @var  mixed
  59.     */
  60.     var $options = false;
  61.  
  62.     /**
  63.     * Fetchmode
  64.     *
  65.     * @var  integer
  66.     */
  67.     var $fetchmode = DB_FETCHMODE_ASSOC;
  68.  
  69.     /**
  70.     * Fetchmode Object Class
  71.     *
  72.     * @var  string
  73.     */
  74.     var $fetchmode_object_class = 'DB_row';
  75.  
  76.     /**
  77.     * Constructor
  78.     *
  79.     * @param    string  Name of container class
  80.     * @param    array   Array with container class options
  81.     * @param    integer Lifetime of a cached result set (in seconds)
  82.     */
  83.     function Cache_DB($container = 'file',
  84.                       $container_options = array(
  85.                         'cache_dir'       => '.',
  86.                         'filename_prefix' => 'query_'
  87.                       ),
  88.                       $expires = 3600) {
  89.         $this->Cache($container, $container_options);
  90.         $this->expires = $expires;      
  91.     }
  92.  
  93.     /**
  94.     * PEAR-Deconstructor
  95.     * Call deconstructor of parent,
  96.     * close database connection if open
  97.     */
  98.     function _Cache_DB() {
  99.         $this->_Cache();
  100.  
  101.         if (is_object($this->db)) {
  102.             $this->db->disconnect();
  103.         }
  104.     }
  105.  
  106.     /**
  107.     * Connect to a database.
  108.     *
  109.     * @param  string  PEAR DB DSN for the database connection
  110.     * @param  mixed   options
  111.     * @throws object  DB_Error
  112.     */
  113.     function connect($dsn, $options = false) {
  114.         if (!isset($this->db)) {
  115.             $this->db = DB::connect($dsn, $options);
  116.         
  117.             if (DB::isError($this->db)) {
  118.                 return $this->db;
  119.             }
  120.         }
  121.     }
  122.  
  123.     /**
  124.     * Register a database connection for connect on demand.
  125.     *
  126.     * @param  string  PEAR DB DSN for the database connection
  127.     * @param  mixed   options
  128.     */
  129.     function setConnection($dsn, $options = false) {
  130.       $this->dsn     = $dsn;
  131.       $this->options = $options;
  132.     }
  133.  
  134.     /**
  135.     * Sets which fetch mode should be used by default on queries
  136.     * on this connection.
  137.     *
  138.     * @param integer  DB_FETCHMODE_ASSOC, DB_FETCHMODE_OBJECT or
  139.     *                 DB_FETCHMODE_ORDERED
  140.     *
  141.     * @param string   The class of the object to be returned by
  142.     *                 the fetch methods when the DB_FETCHMODE_OBJECT
  143.     *                 mode is selected.
  144.     */
  145.     function setFetchMode($fetchmode, $object_class = null) {
  146.         switch ($fetchmode) {
  147.             case DB_FETCHMODE_OBJECT: {
  148.                 if ($object_class) {
  149.                     $this->fetchmode_object_class = $object_class;
  150.                 }
  151.             }
  152.  
  153.             case DB_FETCHMODE_ORDERED:
  154.             case DB_FETCHMODE_ASSOC: {
  155.                 $this->fetchmode = $fetchmode;
  156.             }
  157.             break;
  158.         }
  159.     }
  160.  
  161.     /**
  162.     * Perform an SQL query.
  163.     *
  164.     * @param  string  SQL Query String
  165.     * @return object  Cache_DB_Result
  166.     * @throws object  Cache_Error
  167.     */
  168.     function &query($query) {
  169.         if (stristr($query, 'SELECT')) {
  170.             $cache_id = md5($query);
  171.  
  172.             $result = $this->get($cache_id, 'db_cache');
  173.  
  174.             if ($result == NULL) {
  175.                 if (!isset($this->db)) {
  176.                     if (!empty($this->dsn)) {
  177.                         $this->connect($this->dsn, $this->options);
  178.                     } else {
  179.                         return new Cache_Error(
  180.                           'No database connection. Either open a connection ' .
  181.                           'using connect() or register a connection with ' .
  182.                           'setConnection($dsn, $options)',
  183.                           __FILE__,
  184.                           __LINE__
  185.                         );
  186.                     }
  187.                 }
  188.  
  189.                 $_result = $this->db->query($query);
  190.  
  191.                 if (!DB::isError($_result)) {
  192.                     $rows = array();
  193.  
  194.                     while ($row = $_result->fetchRow(DB_FETCHMODE_ASSOC)) {
  195.                         $rows[] = $row;
  196.                     }
  197.  
  198.                     $result = new Cache_DB_Result(
  199.                       $rows,
  200.                       $this->fetchmode,
  201.                       $this->fetchmode_object_class
  202.                     );
  203.  
  204.                     $this->save($cache_id, $result, $this->expires, 'db_cache');
  205.                 } else {
  206.                     return $_result;
  207.                 }
  208.             }
  209.         } else {
  210.             if (!isset($this->db)) {
  211.                 if (!empty($this->dsn)) {
  212.                     $this->connect($this->dsn, $this->options);
  213.                     $result = $this->db->query($query);
  214.                 } else {
  215.                     return new Cache_Error(
  216.                       'No database connection. Either open a connection ' .
  217.                       'using connect() or register a connection with ' .
  218.                       'setConnection($dsn, $options)',
  219.                       __FILE__,
  220.                       __LINE__
  221.                     );
  222.                 }
  223.             }
  224.         }
  225.  
  226.         return $result;
  227.     }
  228. }
  229.  
  230. /**
  231. * Cache_DB_Result
  232. *
  233. * @author       Sebastian Bergmann <sb@sebastian-bergmann.de>
  234. * @module       Cache_DB
  235. * @modulegroup  Cache_DB
  236. * @package      Cache
  237. * @version      $Revision: 1.7 $
  238. * @access       public
  239. */
  240. class Cache_DB_Result {
  241.     /**
  242.     * Names of the result set's columns
  243.     *
  244.     * @var  array
  245.     */
  246.     var $column_names = array();
  247.  
  248.     /**
  249.     * Cursor
  250.     *
  251.     * @var  integer
  252.     */
  253.     var $cursor = 0;
  254.  
  255.     /**
  256.     * Fetchmode
  257.     *
  258.     * @var  integer
  259.     */
  260.     var $fetchmode = DB_FETCHMODE_ASSOC;
  261.  
  262.     /**
  263.     * Fetchmode Object Class
  264.     *
  265.     * @var  string
  266.     */
  267.     var $fetchmode_object_class = 'DB_row';
  268.  
  269.     /**
  270.     * Number of columns in the result set
  271.     *
  272.     * @var  integer
  273.     */
  274.     var $num_columns = 0;
  275.  
  276.     /**
  277.     * Number of rows in the result set
  278.     *
  279.     * @var  integer
  280.     */
  281.     var $num_rows = 0;
  282.  
  283.     /**
  284.     * Rows of the result set
  285.     *
  286.     * @var  array
  287.     */
  288.     var $rows = array();
  289.  
  290.     /**
  291.     * Constructor
  292.     *
  293.     * @param  array   rows
  294.     * @param  integer fetchmode
  295.     * @param  string  fetchmode_object_class
  296.     */
  297.     function Cache_DB_Result(&$rows, $fetchmode, $fetchmode_object_class) {
  298.         $this->rows                   = $rows;
  299.         $this->fetchmode              = $fetchmode;
  300.         $this->fetchmode_object_class = $fetchmode_object_class;
  301.  
  302.         $this->column_names = array_keys($this->rows[0]);
  303.         $this->cursor       = 0;
  304.         $this->num_columns  = sizeof($this->column_names);
  305.         $this->num_rows     = sizeof($this->rows);
  306.     }
  307.  
  308.     /**
  309.      * Fetch and return a row of data.
  310.      * @param   integer format of fetched row
  311.      * @param   mixed   row to fetch
  312.      * @return  mixed   a row of data, NULL on no more rows
  313.      * @throws  object  DB_Error
  314.      */
  315.     function fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null) {
  316.         if ($fetchmode === DB_FETCHMODE_DEFAULT) {
  317.             $fetchmode = $this->fetchmode;
  318.         }
  319.  
  320.         if ($fetchmode === DB_FETCHMODE_OBJECT) {
  321.             $fetchmode = DB_FETCHMODE_ASSOC;
  322.             $return_object = true;
  323.         }
  324.   
  325.         if ($rownum === null) {
  326.             $this->cursor++;
  327.         } else {
  328.             $this->cursor = $rownum;
  329.         }
  330.  
  331.         if ($rownum < sizeof($this->rows)) {
  332.             $row = $this->rows[$this->cursor];
  333.         } else {
  334.             return false;
  335.         }
  336.  
  337.         switch ($fetchmode) {
  338.             case DB_FETCHMODE_ASSOC: {
  339.                 if (isset($return_object)) {
  340.                     $class  =  $this->fetchmode_object_class;
  341.                     $object =& new $class($row);
  342.  
  343.                     return $object;
  344.                 } else {
  345.                     return $row;
  346.                 }
  347.             }
  348.             break;
  349.  
  350.             case DB_FETCHMODE_ORDERED: {
  351.                 $_row = array();
  352.  
  353.                 foreach ($this->column_names as $column_name) {
  354.                     $_row[] = $row[$column_name];
  355.                 }
  356.  
  357.                 return $_row;
  358.             }
  359.             break;
  360.  
  361.             default: {
  362.                return false;
  363.             }
  364.         }
  365.     }
  366.  
  367.     /**
  368.      * Fetch a row of data into an existing variable.
  369.      *
  370.      * @param   array   reference to data containing the row
  371.      * @param   integer format of fetched row
  372.      * @param   integer row number to fetch
  373.      * @return  mixed   DB_OK on success, NULL on no more rows
  374.      */
  375.     function fetchInto(&$row, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null) {
  376.         if ($row = $this->fetchRow($fetchmode, $rownum)) {
  377.             return DB_OK;
  378.         } else {
  379.             return NULL;
  380.         }
  381.     }
  382.  
  383.     /**
  384.      * Get the the number of columns in a result set.
  385.      *
  386.      * @return integer  number of columns
  387.      */
  388.     function numCols() {
  389.         return $this->num_columns;
  390.     }
  391.  
  392.     /**
  393.      * Get the number of rows in a result set.
  394.      *
  395.      * @return integer  number of rows
  396.      */
  397.     function numRows() {
  398.         return $this->num_rows;
  399.     }
  400.  
  401.     /**
  402.      * Frees the resources allocated for this result set.
  403.      */
  404.     function free() {
  405.         $this->column_names = array();
  406.         $this->rows         = array();
  407.         $this->num_columns  = 0;
  408.         $this->num_rows     = 0;
  409.     }
  410.  
  411.     /**
  412.      * tableInfo() is not implemented in the PEAR Cache DB module.
  413.      * @param   mixed   $mode
  414.      * @throws  object  Cache_Error
  415.      */
  416.     function tableInfo($mode = null) {
  417.         return new Cache_Error(
  418.           'tableInfo() is not implemented in the ' .
  419.           'PEAR Cache DB module.',
  420.           __FILE__,
  421.           __LINE__
  422.         );
  423.     }
  424. }
  425. ?>
  426.